package study.仪器解析.串口化通信.生化仪;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 * @Author 刘伟涛
 * @Date 2024/8/7 14:07
 **/
public class LISIntegration {
    public void sendDataToLIS(String data) throws Exception {
        // 设置请求的URL
        URL url = new URL("http://localhost:8001");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法和属性
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Content-Type", "application/json; utf-8");
        connection.setRequestProperty("Accept", "application/json");
        connection.setDoOutput(true);

        // 发送数据
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = data.getBytes(StandardCharsets.UTF_8);
            os.write(input, 0, input.length);
        }

        // 获取响应
        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            System.out.println("Data sent successfully to LIS");
        } else {
            System.out.println("Failed to send data. Response Code: " + responseCode);
        }

        connection.disconnect();
    }

    public static void main(String[] args) {
        LISIntegration lisIntegration = new LISIntegration();
        try {
            // 示例数据
            String jsonData = "{\"key\":\"value\"}";



            lisIntegration.sendDataToLIS(jsonData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
